package com.xysd.express.activity.logistics;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.util.Log;

import com.google.gson.Gson;
import com.xysd.express.R;
import com.xysd.express.adapter.LogisticsPagerAdapter;
import com.xysd.express.base.BaseActivity;
import com.xysd.express.base.BaseFragment;
import com.xysd.express.bean.CarInfoObj;
import com.xysd.express.fragment.LogisticsFragment;
import com.xysd.express.util.JSONParser;
import com.xysd.express.util.ToastUtil;
import com.xysd.express.util.http.HttpCallBack;
import com.xysd.express.util.http.HttpUtil;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.activity_logistics)
public class LogisticsActivity extends BaseActivity {

    @ViewInject(R.id.tab_layout)
    private TabLayout tabLayout;
    @ViewInject(R.id.viewPager)
    private ViewPager viewPager;
    List<BaseFragment>fragments=new ArrayList<>();

    String[] titles={"小货车","中货车","大货车"};

    List<CarInfoObj.CarInfo> carInfos;

    @Override
    protected void init() {
        setTitleBar("物流");
        getData();

    }

    private void getData() {
        HttpUtil.getCarInfo(new HttpCallBack() {
            @Override
            public void onSuccess(String result) {
                Log.i("获取车辆信息",result);
                String code = JSONParser.getStringFromJsonString("Status",result);
                String msg = JSONParser.getStringFromJsonString("Msg",result);
                if ("0".equals(code)){
                    carInfos=new Gson().fromJson(result,CarInfoObj.class).Data;
                    initViewPager();
                    initTablayout();
                }else {
                    ToastUtil.show(msg);
                }
            }

            @Override
            public void onFailure(String error) {
                Log.e("获取车辆信息",error);
            }
        });
    }

    private void initViewPager() {
        fragments.add(new LogisticsFragment().setType("small",carInfos.get(0)));
        fragments.add(new LogisticsFragment().setType("middle",carInfos.get(1)));
        fragments.add(new LogisticsFragment().setType("large",carInfos.get(2)));
        viewPager.setAdapter(new LogisticsPagerAdapter(getSupportFragmentManager(),mActivity,fragments));

    }

    private void initTablayout() {
        tabLayout.addTab(tabLayout.newTab().setText("小货车"));
        tabLayout.addTab(tabLayout.newTab().setText("中货车"));
        tabLayout.addTab(tabLayout.newTab().setText("大货车"));
        tabLayout.setupWithViewPager(viewPager);
        //如果不重新设置title，tab文字将变成空
        tabLayout.getTabAt(0).setText(titles[0]);
        tabLayout.getTabAt(1).setText(titles[1]);
        tabLayout.getTabAt(2).setText(titles[2]);
    }
}
